package com.pda.common_easy.dialog.product;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.pda.common_easy.PDApp;
import com.pda.common_easy.R;
import com.pda.common_easy.beanout.ProductBeanOut;
import com.pda.common_easy.beanout.StoreBeanOut;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

public class ProductAllDialog extends Dialog {
    private String TAG = "ProductDialog";
    private ImageView ivexit;
    private SmartRefreshLayout refreshLayout;
    private EditText etsn;
    private ListView listView;
    private TextView tvt;
    private PriorityListener listener;
    private Context mContext;
    private ProductAdapter productAdapter;
    private List<ProductBeanOut.DataBean> mlist = new ArrayList<>();
    private View layout;

    public interface PriorityListener {
        void setActivityText(String str,String str1,int num);
    }

    public ProductAllDialog(final Context context, int theme, PriorityListener priorityListener) {
        super(context, theme);
        layout = View.inflate(context, R.layout.dialog_product, null);
        setContentView(layout);
        mContext = context;
        listener = priorityListener;
        ivexit = (ImageView) layout.findViewById(R.id.ivexit);
        ivexit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        etsn = (EditText) layout.findViewById(R.id.eid);
        etsn.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(PDApp.productBeanOutt.getData()==null){
                    return;
                }
                mlist.clear();
                if(s.toString().length()==0){
                    mlist.addAll(PDApp.productBeanOutt.getData());
                }else {
                    for (int i = 0; i <PDApp.productBeanOutt.getData().size() ; i++) {
                        if(PDApp.productBeanOutt.getData().get(i).getCode().contains(s.toString()) || PDApp.productBeanOutt.getData().get(i).getName().contains(s.toString())){
                            mlist.add(PDApp.productBeanOutt.getData().get(i));
                        }
                    }
                }
                productAdapter.notifyDataSetChanged();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        refreshLayout = (SmartRefreshLayout) layout.findViewById(R.id.refreshLayout);
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableHeaderTranslationContent(false);

        listView = (ListView) layout.findViewById(R.id.listview);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                listener.setActivityText(mlist.get(position).getName(),mlist.get(position).getId(),mlist.get(position).getCaseBoxNum());
                dismiss();
            }
        });
        ProductBeanOut.DataBean dataBean = new ProductBeanOut.DataBean();
        dataBean.setCode("");
        dataBean.setId("-1");
        dataBean.setName("全部产品");
        mlist.add(dataBean);
        if(PDApp.productBeanOutt.getData()!=null){
            mlist.addAll(PDApp.productBeanOutt.getData());
        }
        productAdapter = new ProductAdapter(context,mlist);
        listView.setAdapter(productAdapter);
    }

    @Override
    public void show() {
        PDApp.scanfp = false;
        super.show();
    }

    @Override
    public void dismiss() {
        PDApp.scanfp = true;
        mlist=null;
        super.dismiss();
    }
}


